package ru.spb.osll.myfit.server.handler;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Test;

import ru.spb.osll.myfit.common.Content;
import ru.spb.osll.myfit.common.TreeNode;
import ru.spb.osll.myfit.util.FileHelper;

public class ContentCreatorTest {
	/*
	  Test case:
	        root-+
	             |
	             +-content.txt
		     |
                     +-TestPage1-+
                     |           |
                     |           +-content.txt
	             |
	             +-TestPage2-+
	             |           |
	             |           +-content.txt
	             |           |
	             |           +-TestPage3-+
	             |                       |
	             |                       +-content.txt
	             |
	             +-EmptyDir

	  Expected content tree:
		*-+
		  |
		  +-TestPage1
		  |
		  +-TestPage2-+
		              |
		              +-TestPage3

		prefix: "root"

	 */

  private String root = "FitNesseRoot";
  private String topDir = root + File.separator + "root" + File.separator;
  private String testPage1 = topDir + File.separator + "TestPage1" +
      File.separator;
  private String testPage2 = topDir + File.separator + "TestPage2" +
      File.separator;
  private String testPage3 = testPage2 + File.separator + "TestPage3" +
      File.separator;
  private String emptyDir = topDir + File.separator + "EmptyDir" +
      File.separator;

  private File topDirFile = new File(topDir);

  private File contentTopDir = new File(topDir + "content.txt");
  private File contentTestPage1 = new File(testPage1 + "content.txt");
  private File contentTestPage2 = new File(testPage2 + "content.txt");
  private File contentTestPage3 = new File(testPage3 + "content.txt");

  private File testPage1File = new File(testPage1);
  private File testPage2File = new File(testPage2);
  private File testPage3File = new File(testPage3);
  private File emptyDirFile = new File(emptyDir);

  private File[] topDirFiles = {contentTopDir, testPage1File, testPage2File,
      emptyDirFile};

  private File[] testPage1Files = {contentTestPage1};
  private File[] testPage2Files = {contentTestPage2, testPage3File};
  private File[] testPage3Files = {contentTestPage3};
  private File[] emptyDirFiles = {};


  @Test
  public void testCreateContent() {
    FileHelper helper = EasyMock.createStrictMock(FileHelper.class);

    EasyMock.expect(helper.getFile(topDir + "content.txt")).andReturn(
        contentTopDir);
    EasyMock.expect(helper.listFiles(topDirFile)).andReturn(topDirFiles);
    EasyMock.expect(helper.isDirectory(contentTopDir)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(testPage1File)).andReturn(true);
    EasyMock.expect(helper.listFiles(testPage1File)).andReturn(testPage1Files);
    EasyMock.expect(helper.isDirectory(contentTestPage1)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(testPage2File)).andReturn(true);
    EasyMock.expect(helper.listFiles(testPage2File)).andReturn(testPage2Files);
    EasyMock.expect(helper.isDirectory(contentTestPage2)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(testPage3File)).andReturn(true);
    
    EasyMock.expect(helper.isDirectory(emptyDirFile)).andReturn(true);
    EasyMock.expect(helper.listFiles(emptyDirFile)).andReturn(emptyDirFiles);

    EasyMock.replay(helper);

    ContentCreator cc = new ContentCreator();
    cc.setHelper(helper);
    Content c = cc.createContent(root, "root", new Integer(1));

    EasyMock.verify(helper);

    assertTrue(c != null);
    assertEquals("root", c.getPath());

    TreeNode root = c.getTreeNode();
    List<TreeNode> rootChildren = root.getChildren();
    assertTrue(rootChildren.size() == 3);

    TreeNode testPage1root = rootChildren.get(0);
    assertFalse(testPage1root.hasChild());

    TreeNode testPage2root = rootChildren.get(1);
    List<TreeNode> testPage2Children = testPage2root.getChildren();
    assertTrue(testPage2root.hasChild());
    assertTrue(testPage2Children.size() == 0);
    
    TreeNode emptyDir = rootChildren.get(2);
    List<TreeNode> emptyDirChildren = emptyDir.getChildren();
    assertFalse(emptyDir.hasChild());
    assertTrue(emptyDirChildren.size() == 0);
  }

  @Test
  public void testcreateContentsWithLevelAll() {
    FileHelper helper = EasyMock.createStrictMock(FileHelper.class);

    EasyMock.expect(helper.getFile(topDir + "content.txt")).andReturn(
        contentTopDir);
    EasyMock.expect(helper.listFiles(topDirFile)).andReturn(topDirFiles);
    EasyMock.expect(helper.isDirectory(contentTopDir)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(testPage1File)).andReturn(true);
    EasyMock.expect(helper.listFiles(testPage1File)).andReturn(testPage1Files);
    EasyMock.expect(helper.isDirectory(contentTestPage1)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(testPage2File)).andReturn(true);
    EasyMock.expect(helper.listFiles(testPage2File)).andReturn(testPage2Files);
    EasyMock.expect(helper.isDirectory(contentTestPage2)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(testPage3File)).andReturn(true);
    EasyMock.expect(helper.listFiles(testPage3File)).andReturn(testPage3Files);
    EasyMock.expect(helper.isDirectory(contentTestPage3)).andReturn(false);
    
    EasyMock.expect(helper.isDirectory(emptyDirFile)).andReturn(true);
    EasyMock.expect(helper.listFiles(emptyDirFile)).andReturn(emptyDirFiles);

    EasyMock.replay(helper);

    ContentCreator cc = new ContentCreator();
    cc.setHelper(helper);
    Content c = cc.createContent(root, "root", new Integer(3));

    EasyMock.verify(helper);

    assertTrue(c != null);
    assertEquals("root", c.getPath());

    TreeNode root = c.getTreeNode();
    List<TreeNode> rootChildren = root.getChildren();
    assertTrue(rootChildren.size() == 3);

    TreeNode testPage1root = rootChildren.get(0);
    assertFalse(testPage1root.hasChild());

    TreeNode testPage2root = rootChildren.get(1);
    List<TreeNode> testPage2Children = testPage2root.getChildren();
    assertTrue(testPage2root.hasChild());
    assertTrue(testPage2Children.size() == 1);
    
    TreeNode testPage3root = testPage2Children.get(0);
    assertFalse(testPage3root.hasChild());
  }

  @Test
  public void testcreateContentEmpty() {
    FileHelper helper = EasyMock.createStrictMock(FileHelper.class);

    EasyMock.expect(helper.getFile(topDir + "content.txt")).andReturn(
        contentTopDir);
    EasyMock.expect(helper.listFiles(topDirFile)).andReturn(null);

    EasyMock.replay(helper);

    ContentCreator cc = new ContentCreator();
    cc.setHelper(helper);
    Content c = cc.createContent(root, "root", 4);

    assertNotNull(c);
    assertEquals("root", c.getPath());

    TreeNode root = c.getTreeNode();
    List<TreeNode> rootChildren = root.getChildren();
    assertTrue(rootChildren.isEmpty());

    EasyMock.verify(helper);

  }

}
